package com.suyang.syt.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.UserDao;
import com.suyang.syt.dao.interfaces.UserVisitorDao;
import com.suyang.syt.model.User;
import com.suyang.syt.model.UserVisitor;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.model.dto.UserVisitorDTO;
import com.suyang.syt.service.interfaces.UserVisitorService;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.StringUtil;

@Service
public class UserVisitorServiceImpl implements UserVisitorService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private UserVisitorDao userVisitorDao;

	@Override
	public AjaxObj add(long vid, long uid, String remoteAddr, String status) throws Exception {
		if (status.equals("0")) {// 用户
			User user = userDao.load(uid);
			if (user == null) {
				return new AjaxObj(0, "被访问者加载失败!");
			}
		}
		User vuser = userDao.load(vid);
		if (vuser == null) {
			return new AjaxObj(0, "访问者加载失败!");
		}
		UserVisitor userVisitor = new UserVisitor(vid, uid, remoteAddr);
		userVisitor.setCreateTime(DateFormat.dateToString(new Date()));
		userVisitor.setStatus(Integer.parseInt(status));
		userVisitor.setCreateName(vuser.getNickname());
		userVisitorDao.add(userVisitor);
		return new AjaxObj(1, "添加用户访问成功!");
	}

	@Override
	public AjaxObj delete(String ids, String status) throws Exception {
		userVisitorDao.deleteByIds(ids);
		return new AjaxObj(1, "删除用户访问成功!");
	}

	@Override
	public PagerEasyUI<UserVisitorDTO> listByEasyUI(String uid, int page, int rows, String sort, String order, String createDate1, String createDate2, String status) throws Exception {
		StringBuilder hql = new StringBuilder(" from UserVisitor uv where 1=1  ");
		if (EmptyUtil.isNotEmpty(createDate1)) {
			hql.append(" and uv.createTime>='").append(createDate1).append("'");
		}
		if (EmptyUtil.isNotEmpty(createDate2)) {
			hql.append(" and uv.createTime<='").append(createDate2).append("'");
		}
		if (EmptyUtil.isNotEmpty(uid)) {
			hql.append(" and uv.uid = ").append(Long.parseLong(uid));
		}
		if (EmptyUtil.isNotEmpty(status)) {
			hql.append(" and uv.status = ").append(Long.parseLong(status));
		}
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by uv.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<UserVisitorDTO> easyUIPager = new PagerEasyUI<UserVisitorDTO>();
		List<UserVisitor> userVisitors = userVisitorDao.findByEasyUI(hql.toString(), page, rows);
		List<UserVisitorDTO> datas = new ArrayList<UserVisitorDTO>();
		UserVisitorDTO userVisitorDTO = null;
		User vuser = null;
		for (UserVisitor userVisitor : userVisitors) {
			userVisitorDTO = new UserVisitorDTO(userVisitor);
			vuser = userDao.load(userVisitor.getVid());
			if (vuser != null) {
				userVisitorDTO.setVname(vuser.getNickname());
				userVisitorDTO.setVphone(StringUtil.replacePhone(vuser.getPhone()));
			}
			datas.add(userVisitorDTO);
		}
		easyUIPager.setRows(datas);
		easyUIPager.setTotal((Long) userVisitorDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		return easyUIPager;
	}

	@Override
	public PagerEasyUI<UserVisitorDTO> listByEasyUI(long uid, int page, int rows, String sort, String order, String createDate1, String createDate2, String status) throws Exception {
		StringBuilder hql = new StringBuilder(" from UserVisitor uv where 1=1  ");
		if (EmptyUtil.isNotEmpty(createDate1)) {
			hql.append(" and uv.createTime>='").append(createDate1).append("'");
		}
		if (EmptyUtil.isNotEmpty(createDate2)) {
			hql.append(" and uv.createTime<='").append(createDate2).append("'");
		}
		if (uid != 0) {
			hql.append(" and uv.uid = ").append(uid);
		}
		if (EmptyUtil.isNotEmpty(status)) {
			hql.append(" and uv.status = ").append(Long.parseLong(status));
		}
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by uv.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<UserVisitorDTO> easyUIPager = new PagerEasyUI<UserVisitorDTO>();
		List<UserVisitor> userVisitors = userVisitorDao.findByEasyUI(hql.toString(), page, rows);
		List<UserVisitorDTO> datas = new ArrayList<UserVisitorDTO>();
		UserVisitorDTO userVisitorDTO = null;
		User vuser = null;
		for (UserVisitor userVisitor : userVisitors) {
			userVisitorDTO = new UserVisitorDTO(userVisitor);
			vuser = userDao.load(userVisitor.getVid());
			if (vuser != null) {
				userVisitorDTO.setVname(vuser.getNickname());
				userVisitorDTO.setVphone(StringUtil.replacePhone(vuser.getPhone()));
			}
			datas.add(userVisitorDTO);
		}
		easyUIPager.setRows(datas);
		easyUIPager.setTotal((Long) userVisitorDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		return easyUIPager;
	}

	@Override
	public List<UserVisitor> list(String ids) throws Exception {
		String hql = " from UserVisitor b where b.id in(" + ids + ")";
		return userVisitorDao.list(hql);
	}

}
